//
//  YonghusousuoViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "YonghusousuoViewController.h"
#import "YonghuSTableViewCell.h"
#import "PersonDetailViewController.h"
@interface YonghusousuoViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,optionDelegate>
{
    UITableView * contentTableView;
    NSArray * dataArr;
    UITextField * search;
}
@end

@implementation YonghusousuoViewController


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(60, 0, ScreenWidth - 70, 30)];
    view.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    view.layer.cornerRadius = 15;
    self.navigationItem.titleView = view;
    
    search = [[UITextField alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth - 100, 30)];
    search.placeholder = @"用户搜索及添加";
    search.font = [UIFont systemFontOfSize:15];
    search.delegate = self;
    search.returnKeyType = UIReturnKeySearch;
    [view addSubview:search];
    
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight - rectStatus.size.height - rectNav.size.height) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    
    [self request];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (![textField.text isEqualToString:@""])
    {
        [self search];
    }
    else
    {
        [self request];
    }
    [textField resignFirstResponder];
    return YES;
}

-(void)request
{
    [NetRequestCenter getfrlistWithFriendName:search.text page:@"1" Result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
            [contentTableView reloadData];
        }
    }];
}

-(void)search
{
    [NetRequestCenter getfrlistWithFriendName:search.text page:@"1" Result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            dataArr = [[resultDic objectForKey:@"data"]objectForKey:@"Detail"];
            [contentTableView reloadData];
        }
    }];
}



#pragma mark - tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArr  count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"好友列表";
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [YonghuSTableViewCell height];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifer = @"identifer";
    YonghuSTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell = [[YonghuSTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.delegate = self;
    [cell loadData:[dataArr objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PersonDetailViewController * per = [[PersonDetailViewController alloc]init];
    per.uid = [[dataArr objectAtIndex:indexPath.row]objectForKey:@"FriendUid"];
    [self.navigationController pushViewController:per animated:YES];
}

-(void)deleteAction:(NSString *)userId
{
    [SVProgressHUD show];
    [NetRequestCenter delfriendWithpage:nil friendname:nil uid:userId result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            [search resignFirstResponder];
            search.text = @"";
            [self request];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)adadAction:(NSString *)userId
{
    [SVProgressHUD show];
    [NetRequestCenter makefriendWithpage:@"" friendname:@"" uid:userId result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            [SVProgressHUD showSuccessWithStatus:@"添加成功"];
            [search resignFirstResponder];
            search.text = @"";
            [self request];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
